﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using Castle.MonoRail.Framework;
using GettingStartedSample.Models;
using Sport.Site.Models;

namespace Sport.Site.Controllers
{
	[Layout("default"), Rescue("generalerror")]
	public class PlayerController : SmartDispatcherController
	{
		public void New()
		{
		}

		public void Home(Int64 id)
		{
			PushDataIntoView(Player.FindById(id));
		}

		public void FindMeeting(Int64 id, Int64 venueId, Int64 activityId)
		{
			var activity = Activity.FindById(activityId);
			var venue = Venue.FindById(venueId);
			var foundMeetings = Meeting.FindBy(venue, activity);

			var json = ToJson(id, foundMeetings);
			RenderText(json);
		}

		public void SignUp(Int64 id, Int64 meetingId)
		{
			var meeting = Meeting.FindById(meetingId);
			var player = Player.FindById(id);
			var playerAdded = meeting.AddPlayer(player);
			meeting.Save();

			var json = playerAdded ? ToJson(id, new List<Meeting> {meeting}) : ToJson(id, new List<Meeting> {});
			RenderText(json);
		}

		private string ToJson(Int64 playerId, IList<Meeting> meetings)
		{
			var sb = new StringBuilder();
			sb.Append("{\"PlayerId\": " + playerId + ",");
			sb.Append("\"Meetings\": [");
			foreach (var meeting in meetings)
				AddMeetingEntry(sb, meeting);
			var s = sb.ToString();
			var start = s.TrimEnd(',');
			return start + "]}";
		}

		private void AddMeetingEntry(StringBuilder sb, Meeting meeting)
		{
			sb.Append("{");
			sb.Append("\"Activity\": \"" + meeting.Activity.Name + "\",");
			sb.Append("\"Description\": \"" + meeting.Description + "\",");
			sb.Append("\"Venue\": \"" + meeting.Venue.Name + "\",");
			sb.Append("\"Date\": \"" + meeting.Date + "\",");
			sb.Append("\"Host\": \"" + meeting.Host.Tag + "\",");
			sb.Append("\"Id\": \"" + meeting.Id + "\"");
			sb.Append("},");
		}

		public void NewMeeting(Int64 id, Int64 venueId, Int64 activityId, string description)
		{
			var activity = Activity.FindById(activityId);
			var venue = Venue.FindById(venueId);
			var date = DateTime.Now;
			var player = Player.FindById(id);
			var meeting = new Meeting(player, venue, activity, date, description ?? "");
			meeting.Create();

			player.HostMeeting(meeting);

			var val = new NameValueCollection {{"id", id.ToString()}};
			RedirectToAction("home", val);
		}

		private void PushDataIntoView(Player player)
		{
			PropertyBag["loggedIn"] = true;
			PropertyBag["activities"] = Activity.FindAll();
			PropertyBag["venues"] = Venue.FindAll();
			PropertyBag["player"] = player;
			PropertyBag["my_meetings"] = player.AllMeetings;
		}

		public void Delete(int id)
		{
			var player = Player.FindById(id);
			player.Delete();
			RedirectToAction("list");
		}

		public void Create([DataBind("player")] Player player)
		{
			player.Create();
			RedirectToAction("list");
		}

		public void List()
		{
			PropertyBag["players"] = Player.FindAll();
		}

		public void Edit(int id)
		{
			PropertyBag["player"] = Player.FindById(id);
		}

		public void Update([DataBind("player")] Player player)
		{
			try
			{
				player.Update();
				RedirectToAction("list");
			}
			catch (Exception ex)
			{
				Flash["error"] = ex.Message;
				Flash["player"] = player;

				RedirectToAction("edit", "id=" + player.Id);
			}
		}
	}
}